import { _decorator, Component, Node, ScrollView, Prefab, Label } from 'cc';
import { GameData } from "./framework/GameData";
import { PoolManager } from './framework/poolManager';
// import { ResourceUtilClass } from './framework/resourceUtilClass';
import { MatchMap } from "./MatchMap";
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
const { ccclass, property } = _decorator;

@ccclass('MatchListPanel')
export class MatchListPanel extends Component {
    // @property(Node)
    // public rootNode: Node = null!;//根节点

    @property(Node)
    public rootLayout: Node = null!;//滚动列表根节点

    @property(ScrollView)
    public scrollView: ScrollView = null!;//滚动视图

    onLoad() {
        // this.rootNode = this.node.parent;
        GameData.getInstance().rootNode = this.node.parent;
    }

    start() {
        GameData.getInstance().loadConfig().then(async () => {
            console.log("MatchListPanel onLoad");

            // 排行榜 先清除列表
            let children = this.rootLayout.children;
            for (let i = children.length - 1; i >= 0; i--) {
                PoolManager.instance.putNode(children[i]);
            }

            // 新增子项
            // 残局每一个具体残局的预设
            // let prefab = await ResourceUtilClass.loadTheRes("prefab/matchMap", Prefab) as Prefab;
            // const prefab = await bdlMgr.getRes<Prefab>(`resources`, `prefab/matchMap`, Prefab);
            const prefab = await bdlMgr.getRes<Prefab>(`Xiangqi_Prefab`, `matchMap`, Prefab);

            let list = GameData.getInstance().match_data;
            for (let i = 0; i < list.length; i++) {
                let node = PoolManager.instance.getNode(prefab, this.rootLayout);
                let title = node.getChildByName("root")!.getChildByName("title");
                title.getComponent(Label)!.string = list[i].name;
                let num = node.getChildByName("root")!.getChildByName("num");
                num.getComponent(Label)!.string = i + 1 + ".";
                let script = node.getComponent(MatchMap);
                script.setMatchId(i + 1);
            }

            // 滚到最上面
            this.scrollView.scrollToTop();
        });
    }

    onClickReturn() {
        // 先销毁
        this.node.removeFromParent();

        // 隐藏主界面
        let mainui = GameData.getInstance().rootNode.getChildByName("mainui");
        mainui.active = true;
    }

    update(deltaTime: number) {

    }
}

